* Firm organization with multiple establishments
* Section III.A: Appendix Table B.1

clear all 
set matsize 2000
set more off, perm

capture log close
log using log/06_facts-p_MElocation.log, replace

use $data/MEloc_2000-2010_p.dta, clear
desc

label variable ldistall "Log distance to HQ"
label variable ln_MP "Log market potential"
label variable rel_wage "Relative wages"
label variable rel_Preis "Relative land prices"

********************************************************************************
***	TABLE B.1: Location probability - columns 1-3

eststo probit1, title("probit1"): qui probit inv ldistall ln_MP rel_wage d_year* d_legal* d_93wz* d_hqkreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo probit2, title("probit2"): qui probit inv ldistall ln_MP rel_wage rel_Preis d_year* d_legal* d_93wz* d_hqkreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo probit3, title("probit3"): qui probit inv ldistall d_aokreis* d_year* d_legal* d_93wz* d_hqkreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

esttab probit1 probit2 probit3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	varwidth(24) stats(N N_clust count_unt, fmt(%18.0g %9.0g %9.0g) label("\# observations" "\# HQ counties" "\# firms")) ///
	compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label ///
	indicate("Year FE = d_year*" "Legal form FE = d_legal*" "HQ sector FE = d_93wz*" "HQ county FE = d_hqkreis*" "County FE = d_aokreis*", labels(Y N)) 
capture drop _est_*

********************************************************************************
***	TABLE B.1: Log # est. employees - columns 4-6

//	Within firm-year, thus HQ sector, HQ county, legal form FE absorbed

keep if inv == 1
drop inv

sort untid jahr
egen untjhr_id = group(untid jahr)

xtset, clear
xtset untjhr_id ao_kreis

eststo within1, title("within1"): qui xtivreg2 ln_empl_kreis ldistall ln_MP rel_wage if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo within2, title("within2"): qui xtivreg2 ln_empl_kreis ldistall ln_MP rel_wage rel_Preis if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo within3, title("within3"): qui xtivreg2 ln_empl_kreis ldistall d_aokreis* if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

esttab within1 within2 within3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	stats(N N_clust1 N_clust2 count_unt, fmt(%9.0f %9.0f %9.0f %9.0f)) compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label ///
	drop(d_aokreis*)
capture drop _est_*

***	TABLE B.1: Location probability and establishment size, ME firms, 2000-2010 data (complete)
esttab probit1 probit2 probit3 within1 within2 within3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	varwidth(24) stats(N count_unt, fmt(%18.0g %9.0g) label("\# observations" "\# firms")) ///
	compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label ///
	indicate("Legal form FE = d_legal*" "HQ sector FE = d_93wz*" "HQ county FE = d_hqkreis*" "County FE = d_aokreis*", labels(Y N) ) 
capture drop _est_*

erase data/MEloc_2000-2010_p.dta
	
log close
